package com.javaAdmin.config;

import com.javaAdmin.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author Administrator
 */
//@Component 标注一个类为Spring容器的Bean，（把普通pojo实例化到spring容器中，相当于配置文件中的<bean id="" class=""/>）

@Component
public class WebAppConfig implements WebMvcConfigurer {

    @Value("${upload.path}")
    private String uploadPath;

    //验证
    @Resource
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //自定义静态目录
        if (!registry.hasMappingForPattern("/static/**")) {
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        }

        //自定义上传目录
        registry.addResourceHandler("/upload/**").addResourceLocations("file:" + uploadPath);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 默认拦截所有url
        registry.addInterceptor(tokenInterceptor);
//                // 匹配需要拦截的url
//                .addPathPatterns("/api")
//                // 匹配不需要拦截的url
//                .excludePathPatterns("/restful/student/*");
    }

}
